package com.example.protestgps;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.hardware.SensorManager;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class NotNullUserID extends FragmentActivity {

	private LocationManager lm;
	GoogleMap myMap;
	public Context con;
	ArrayList<LatLng> mMarkerPoints = new ArrayList<LatLng>();// add
	public LatLng locationchange;
	public double latchange;
	public double lngchange;
	public LatLng des;
	public int click;
	LatLng start2;
	SensorManager sm;
	Button btnemegency;
	Button btnnormal;
	double deslat1;
	double deslng1;
	SQLiteDatabase db;
	double[] test = new double[19];
	int i = 0;
	int j = 0;
	double min;
	Button startvoicere;
	Boolean clickk = false;
	int a = 1;
	TextView textmai;
	String UserID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setwidget();

		int status = GooglePlayServicesUtil
				.isGooglePlayServicesAvailable(getBaseContext());
		if (status != ConnectionResult.SUCCESS) {

			int requestCode = 10;
			Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,
					requestCode);
			dialog.show();
		}

		else {
			checkInternetConnection();
			lm = (LocationManager) getSystemService(LOCATION_SERVICE);
			con = new MainActivity();
			GetLocation getlocal = new GetLocation(lm, this, myMap);
			getlocal.getLatLot();

			final double latstart = getlocal.m_lat;
			final double lngstart = getlocal.m_lot;

			GetLocation local = new GetLocation(lm, this, myMap);
			latchange = local.GetLat();
			lngchange = local.GetLng();
			start2 = new LatLng(latchange, lngchange);

			GetLocation getloc = new GetLocation(lm, this, myMap);
			sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
			SensorAccelerometer SenAc = new SensorAccelerometer(sm);
			double a = SenAc.geta();
			// Log.d("x sonser",""+x );

			if (a > 1) {

				checkInternetConnection();
				if (checkInternetConnection() == true) {
					Intent gotonext = new Intent(getApplicationContext(),
							CountDown.class);
					startActivity(gotonext);

				}

				else {
					SQLiteforSMS sql = new SQLiteforSMS(this);
					List<Members> MebmerList = sql.SelectAllData();
					if (MebmerList == null) {
						Toast.makeText(NotNullUserID.this, "Not found Data!",
								Toast.LENGTH_LONG).show();
					} else {

						Log.d("tttttttttttt", "tttttttt111111111111111");

						for (Members mem : MebmerList) {
							Toast.makeText(
									NotNullUserID.this,
									"MemberID = " + mem.gMemberID() + ","
											+ mem.gLat() + "," + mem.gLng()
											+ "," + mem.gmobileno(),
									Toast.LENGTH_LONG).show();
							if (mem != null) {

								mem.setCurpoint(latchange, lngchange);

							}

						}

						Collections.sort(MebmerList);

						System.out.println("min#2:"
								+ MebmerList.get(0)._distToCurr + "\nmono#1:"
								+ MebmerList.get(0)._mobileno + "\nlat#1: "
								+ MebmerList.get(0)._Lat + "\nlng#1:"
								+ MebmerList.get(0)._Lng);

						System.out.println("min#2:"
								+ MebmerList.get(1)._distToCurr + "\nmono#2:"
								+ MebmerList.get(1)._mobileno + "\nlat#2: "
								+ MebmerList.get(1)._Lat + "\nlng#2:"
								+ MebmerList.get(1)._Lng);

						System.out.println("min#2:"
								+ MebmerList.get(2)._distToCurr + "\nmono#2:"
								+ MebmerList.get(2)._mobileno + "\nlat#2: "
								+ MebmerList.get(2)._Lat + "\nlng#2:"
								+ MebmerList.get(2)._Lng);

						Intent goSMS = new Intent(getApplicationContext(),
								SMSSender.class);
						goSMS.putExtra("Curlat", latchange);
						goSMS.putExtra("Curlng", lngchange);
						goSMS.putExtra("mobileno", MebmerList.get(0)._mobileno);
						startActivity(goSMS);

					}
				}
			}

			btnemegency.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					checkInternetConnection();
					if (checkInternetConnection() == true) {
						Intent gotonext = new Intent(getApplicationContext(),EmerNotNull.class);
						gotonext.putExtra("latchange", latchange);
						gotonext.putExtra("lngchange", lngchange);
						startActivity(gotonext);
						
						
					}

				}
			});

			btnnormal.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub

					Intent gotoIntent = new Intent(getApplicationContext(),
							LisV.class);
					startActivity(gotoIntent);

				}
			});

			startvoicere.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent gotoIntent = new Intent(getApplicationContext(),
							VoiceMsg.class);
					startActivity(gotoIntent);
				}
			});
		}
	}

	private void setwidget() {
		// TODO Auto-generated method stub
		btnemegency = (Button) findViewById(R.id.btnemergency);
		btnnormal = (Button) findViewById(R.id.btnnormal);
		startvoicere = (Button) findViewById(R.id.btnstartvoice);
	}

	private boolean checkInternetConnection() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		// test for connection
		if (cm.getActiveNetworkInfo() != null
				&& cm.getActiveNetworkInfo().isAvailable()
				&& cm.getActiveNetworkInfo().isConnected()) {
			Log.v("test", "test");
			return true;
		} else {
			Log.v("test", "Internet Connection Not Present");
			return false;
		}
	}

}
